﻿package org.davialexandre.asmtp 
{
	import org.davialexandre.asmtp.errors.InvalidHostFormatError;
	import org.davialexandre.asmtp.errors.InvalidPortNumberError;
	
	/**
	 * ...
	 * @author DefaultUser (Tools -> Custom Arguments...)
	 */
	public class SMTPServer 
	{
		private var _host:String;
		private var _port:uint;
		
		public function SMTPServer(host:String, port:uint) 
		{
			this.host = host;
			this.port = port;
		}
		
		public function get host():String {
			return _host;
		}
		
		public function set host(value:String):void {
			var url_pattern:RegExp = new RegExp("^(https?://)"
										+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //user@
										+ "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP- 199.194.52.184
										+ "|" // allows either IP or domain
										+ "([0-9a-z_!~*'()-]+\.)*" // tertiary domain(s)- www.
										+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // second level domain
										+ "[a-z]{2,6})" // first level domain- .com or .museum
										+ "(:[0-9]{1,4})?" // port number- :80
										+ "((/?)|" // a slash isn't required if there is no file name
										+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$", "i");
										
			if (url_pattern.test(value)) {
				_host = value;
			} else {
				throw new InvalidHostFormatError("The URL informed is in a bad format");
			}
		}
		
		public function get port():uint {
			return _port;
		}
		
		public function set port(value:uint):void {
			if (value > -1 && value < 65535) {
				_port = value;
			} else {
				throw new InvalidPortNumberError("You informed a invalid port number. Port number: " + value);
			}
		}
		
	}
	
}